package com.study.activemq;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * Created by andys on 2017/12/10.
 */
public class JMSConsumer {
    public static void main(String[] args) {
        ConnectionFactory connectionFactory;//连接工厂
        Connection connection=null;
        Session session;
        Destination destination;
        MessageConsumer messageConsumer;

        connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,ActiveMQConnection.DEFAULT_BROKER_URL);
        try {
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE);
             destination = session.createQueue("First Queque1");
             messageConsumer = session.createConsumer(destination);
             //接收消息receive方式
             while(true){
                 TextMessage textMessage = (TextMessage)messageConsumer.receive(100000);
                 if (textMessage != null){
                     System.out.println("收到的消息"+textMessage.getText());
                 }else{
                     break;
                 }
             }
        } catch (JMSException e) {
            e.printStackTrace();
        }

    }
}
